package Homework07;

import java.io.*;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.*;

public class Homework02 {
    public static void main(String[] args) {
        String path = "user.txt";

        HashMap<String, String> usersmap = new HashMap<>();
        ArrayList<User> userslist = new ArrayList<>();

        try (BufferedReader br = new BufferedReader(new FileReader(path))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] split = line.split("=");
                if (split.length == 2) {
                    User user = new User();
                    usersmap.put(split[0], split[1]);
                    user.setUsername(split[0]);
                    user.setPassword(split[1]);
                    userslist.add(user);
                }
            }
            //System.out.println(usersmap);
            //System.out.println(userslist);

            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("输入用户名：");
                String name = sc.next();
                boolean nameEquals = nameEquals(name, usersmap);
                User user = getUser(name, userslist);
                if (nameEquals) {
                	if(!user.isFreezeForever()){
                		if (!isfreeze(name, userslist)) {
                            
                            while (true) {
                            	System.out.println("输入密码：");
                                String password = sc.next();
                                boolean passwordEquals = passwordEquals(password, name, usersmap);
                                if (passwordEquals) {
                                	user.setNumOfFreeze(0);
                                    break;
                                } else {
                                	//System.out.println("1."+user.getNumOfAttempts());
                                    int numOfAttempts = user.getNumOfAttempts() + 1;
                                    //System.out.println("2."+numOfAttempts);
                                    user.setNumOfAttempts(numOfAttempts);
                                    if (numOfAttempts == 3) {
                                    	if(user.getNumOfFreeze() == 1){
                                    		user.setFreezeForever(true);
                                    		System.out.println("该账户已被永久冻结");
                                    		break;
                                    	}else{
                                    		 freeze(name, userslist);
                                    		 
                                             break;
                                    	}
                                       
                                    }
                                    continue;
                                }
                            }
                            

                        } else {
                            System.out.println("该账号已被锁，请稍后再试");
                        }
                		
                	}else{
                		System.out.println("该账户已被永久冻结");
                		
                	}
                

                } else {
                    continue;
                }

            }
        } catch (IOException e) {
            System.err.println("读取文件时出现错误：" + e.getMessage());
        }
    }

    private static User getUser(String name, ArrayList<User> userslist) {
        for (User user : userslist) {
            if (user.getUsername().equals(name)) {
                return user;
            }
        }
        return null;
    }

    private static boolean isfreeze(String name, ArrayList<User> userslist) {
        for (User user : userslist) {
            if (user.getUsername().equals(name)) {
                Unlock(user);

                if (user.isFreeze()) {
                    return true;
                }
            }
        }
        return false;
    }

    private static void Unlock(User user) {
        if (user.isFreeze()) {
            LocalDateTime currentTime1 = user.getTime();
            LocalDateTime currentTime2 = LocalDateTime.now();
            long diffInMinutes = ChronoUnit.MINUTES.between(currentTime1, currentTime2);
            if (Math.abs(diffInMinutes) > 1) {
                user.setFreeze(false);
            }
        }
    }

    private static void freeze(String name, ArrayList<User> userslist) {
        for (User user : userslist) {
            if (user.getUsername().equals(name)) {
                user.setFreeze(true);
                user.setNumOfAttempts(0);
                System.out.println("该账户被锁1分钟");
                LocalDateTime currentTime = LocalDateTime.now();
                user.setTime(currentTime);
                int numOfFreeze = user.getNumOfFreeze()+1;
                user.setNumOfFreeze(numOfFreeze);
                
            }
        }
    }

    private static boolean passwordEquals(String password, String name, HashMap<String, String> users) {
        if (password.equals(users.get(name))) {
            System.out.println("登陆成功");
            return true;
        }
        System.out.println("输入密码错误，请重新输入");
        return false;
    }

    private static boolean nameEquals(String name, HashMap<String, String> users) {
        for (String key : users.keySet()) {
            if (key.equals(name)) {
                return true;
            }
        }
        System.out.println("用户不存在");
        return false;
    }
}
